package ui;

import service.IUserService;
import service.UserService;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.HeadlessException;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
public class LoginDialog extends JDialog{		 
	private JLabel label1,label2;
	private JTextField Account;
	private JPasswordField Password;
	private JButton login,register,cancel;
	private JPanel p1;
	public LoginDialog(){

		label1=new JLabel("用户名：");
		Account=new JTextField(15);
		label2=new JLabel(" 密码 ：");
		Password=new JPasswordField(15);
		login=new JButton("登陆");
		register=new JButton("注册");
		cancel=new JButton("取消");

		p1 = (JPanel)this.getContentPane();
		p1=new JPanel();
		p1.add(label1);
		p1.add(Account);//输入用户名
		p1.add(label2);
		p1.add(Password);//输入密码
		p1.add(login);//登录
		p1.add(register);//注册
		p1.add(cancel);

		label1.setFont(new Font("黑体", Font.PLAIN, 20));
		label1.setBounds(95,70,100,30);
		Account.setBounds(180, 70, 150, 30);

		label2.setFont(new Font("黑体", Font.PLAIN, 20));
		label2.setBounds(95,110,100,30);
		Password.setBounds(180, 110, 150, 30);

		login.setBounds(100,170,75,35);
		login.setFont(new Font("宋体", Font.PLAIN, 20));
		login.setForeground(Color.BLUE);
		register.setBounds(185, 170, 75,35);
		register.setForeground(Color.BLUE);
		register.setFont(new Font("宋体", Font.PLAIN, 20));
		cancel.setBounds(270, 170, 75,35);
		cancel.setFont(new Font("宋体", Font.PLAIN, 20));
		cancel.setForeground(Color.BLUE);

		this.add(p1);
		setTitle("用户登陆界面");
		setBounds(100,100,450, 300);
		getContentPane().setLayout(new BorderLayout());
		p1.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(p1, BorderLayout.CENTER);
		p1.setLayout(null);
		setVisible(true);
		setResizable(false);

		login.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				IUserService service;
				MainFrame mainFrame;
				if(Account.getText().trim().equals("")||(Password.getPassword().toString().trim().equals("")))
					JOptionPane.showMessageDialog(null, "用户名和密码不得为空!", "警告！",
								JOptionPane.YES_NO_OPTION);
				else{
				try {
					service=new UserService();
					if(service.checkName(Account.getText(), String.valueOf(Password.getPassword()))){
						setVisible(false);
						mainFrame = new MainFrame();
						mainFrame.setVisible(true);
						mainFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
						
					}
					else
						JOptionPane.showMessageDialog(null, "用户名或密码错误！");
				} catch (Exception e1) {
					e1.printStackTrace();
				}

			}}

		});

		register.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				RegisterDialog rd=new RegisterDialog();
				rd.setVisible(true);
				rd.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
			}

		});
		cancel.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				System.exit(0);
				setVisible(false);
			}

		});
	}
	public static void main(String[] args) {
		 new LoginDialog();
	}
}

